package cn.wolfcode.p2p.bussness.service.impl;

import cn.wolfcode.p2p.base.domain.Account;
import cn.wolfcode.p2p.base.query.QueryObject;
import cn.wolfcode.p2p.base.util.Constants;
import cn.wolfcode.p2p.bussness.domain.AccountFlow;
import cn.wolfcode.p2p.bussness.mapper.AccountFlowMapper;
import cn.wolfcode.p2p.bussness.service.IAccountFlowService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class AccountFlowServiceImpl implements IAccountFlowService {

    @Autowired
    private AccountFlowMapper accountFlowMapper;

    @Override
    public int save(AccountFlow accountFlow) {
        return accountFlowMapper.insert(accountFlow);
    }

    @Override
    public AccountFlow get(Long id) {
        return accountFlowMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo queryPage(QueryObject qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        List<AccountFlow> list = accountFlowMapper.selectList(qo);
        return new PageInfo(list);
    }

    @Override
    public void createRechargeOfflineFlow(Account userAccount, BigDecimal amount, String remark, int actionType) {
        AccountFlow account = new AccountFlow();
        //设置流水时间
        account.setActionTime(new Date());
        //设置流水类型
        account.setActionType(actionType);
        //设置发生的金额
        account.setAmount(amount);
        //设置充值用户账户的ID
        account.setAccountId(userAccount.getId());
        //设置备注信息
        account.setNote(remark);
        //设置充值变化后的金额
        account.setUsableAmount(userAccount.getUsableAmount());
        //设置充值变化后的冻结金额
        account.setFreezedAmount(userAccount.getFreezedAmount());
        //保存到数据库中
        save(account);
    }
}
